package gen.tools.deploy;

import gen.tools.Widgets;
import gen.tools.structures.S2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class CreateDepMain extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CreateDepMain() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		final int depId = wid.getCurrentSN("depmain");									//取得新的部署執行流水號
		final int deplmainSN = Integer.parseInt( request.getParameter("deplmainSN") );	//部署計劃代碼
		final int deliverySN = Integer.parseInt( request.getParameter("deliverySN") );	//交付事件代碼
		final int tarDeliverySN = Integer.parseInt( request.getParameter("tarDeliverySN") );	//交付方的交付事件代碼
		final int targetSystemId =  Integer.parseInt( request.getParameter("targetSystemId") );
		final String empId = request.getParameter("empId");
		final String subsysId = request.getParameter("subsysId");
		final String envId = request.getParameter("envId");
		final String isUrgent = request.getParameter("isUrgent");
		final String sql = "INSERT INTO depmain (depmainSN,deplmainSN,subsysId,envId,creator,createdDateTime,isUrgent,deliverySN) VALUES(?,?,?,?,?,?,?,?)";
		
		final S2 s2 = new S2();
		try {
			conn = wid.getConn();
			
			final int localSysemId = wid.getLocalSysemId();	
			//改變【本地端】的交付狀態	10 部署失敗
			wid.updateDeltaskStatus(deliverySN, 10, wid.getConn() );
			//改變【交付端】的交付狀態	10 部署失敗
			wid.updateDeltaskStatus(tarDeliverySN, 10, wid.getTarConnection(request, targetSystemId) );
			
			final Timestamp t = wid.cDT();
			//處理部署執行主檔
			ps = conn.prepareStatement(sql);
			ps.setInt(1, depId);
			ps.setInt(2, deplmainSN);
			ps.setString(3, subsysId);
			ps.setString(4, envId);
			ps.setString(5, empId);
			ps.setTimestamp(6, t);
			ps.setString(7, isUrgent);
			ps.setInt(8, deliverySN);
			ps.execute();
			
			s2.setS1( String.valueOf(depId) );
			s2.setS2( wid.changeTimeFormatUI(t) );
		} catch(Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( rs !=null ) { rs.close(); }
				if ( ps !=null ) { ps.close(); }
				if ( conn !=null ) { conn.close(); }
			} catch (Exception e) { e.printStackTrace(); }
		}
		
		response.getWriter().write( new Gson().toJson(s2) );
	}

}
